package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;

import net.sf.json.JSONObject;
import cn.wh.xwm.common.util.Constant;
import cn.wh.xwm.common.util.TimeUtils;
import cn.wh.xwm.controller.action.vo.SalesView;
import cn.wh.xwm.hibernate.dao.Customerinfo;
import cn.wh.xwm.hibernate.dao.CustomerinfoDAO;
import cn.wh.xwm.hibernate.dao.Productinfo;
import cn.wh.xwm.hibernate.dao.ProductinfoDAO;
import cn.wh.xwm.hibernate.dao.SalesLiquid;
import cn.wh.xwm.hibernate.dao.SalesLiquidDAO;
import cn.wh.xwm.hibernate.dao.Userinfo;
import cn.wh.xwm.hibernate.dao.UserinfoDAO;

/**
 * sales数据录入
 * 
 * @author swimming
 * 
 */
public class SalesAction extends BaseAction {
	private String page;
	private String rows;
	private String id;
	private String productId;
	private String productName;
	private String patchNumber;
	private String customerFromId;
	private String customerFromName;
	private String customerToId;
	private String customerToName;
	private String amount;
	private String eventDate;

	public void findAllSales() throws IOException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Userinfo userinfo = (Userinfo) session.getAttribute(Constant.USER_SESSION);
		SalesLiquidDAO dao = new SalesLiquidDAO();
		List<SalesLiquid> list = dao.findBypage(page, rows, "SalesLiquid",userinfo.getUserid());
		List<SalesView> vlist = new ArrayList<SalesView>();
		for (SalesLiquid liquid : list) {
			SalesView view = new SalesView();
			view.setId(liquid.getId());
			view.setCustomerFromId(liquid.getCustomerinfoByCustomerIdFrom()
					.getId());
			view.setCustomerFromName(liquid.getCustomerinfoByCustomerIdFrom()
					.getName());
			view
					.setCustomerToId(liquid.getCustomerinfoByCustomerIdTo()
							.getId());
			view.setCustomerToName(liquid.getCustomerinfoByCustomerIdTo()
					.getName());
			view.setEventDate(TimeUtils.toIsoDate(liquid.getEventDate()));
			view.setAmount(String.valueOf(liquid.getAmount()));
			view.setPatchNumber(liquid.getPatchNumber());
			view.setProductId(liquid.getProductinfo().getId());
			view.setProductName(liquid.getProductinfo().getName());
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", dao.findTotalCount("SalesLiquid"));
		dao.getSession().close();
		this.responseJson(object);
	}

	/**
	 * 增加数据
	 * 
	 * @return
	 * @throws IOException
	 * @throws ParseException 
	 */
	public void addData() throws IOException, ParseException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Userinfo userinfo = (Userinfo) session.getAttribute(Constant.USER_SESSION);
		SalesLiquidDAO dao = new SalesLiquidDAO();
		SalesLiquid liquid = new SalesLiquid();
		liquid.setAmount(Integer.valueOf(amount));
		CustomerinfoDAO custdao = new CustomerinfoDAO();
		Customerinfo customerFrom = custdao.findById(customerFromId);
		Customerinfo customerTo = custdao.findById(customerToId);
		liquid.setCustomerinfoByCustomerIdFrom(customerFrom);
		liquid.setCustomerinfoByCustomerIdTo(customerTo);
		liquid.setEventDate(TimeUtils.fromIsoDate(eventDate));
		liquid.setInOrOut(0);
		liquid.setPatchNumber(patchNumber);
		liquid.setUserId(userinfo.getUserid());
		ProductinfoDAO prodao = new ProductinfoDAO();
		Productinfo productinfo = prodao.findById(productId);
		liquid.setProductinfo(productinfo);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.save(liquid);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}

	/**
	 * 修改数据
	 * @throws IOException 
	 * @throws ParseException 
	 */
	public void updateData() throws IOException, ParseException{
		SalesLiquidDAO dao = new SalesLiquidDAO();
		SalesLiquid liquid = dao.findById(id);
		liquid.setAmount(Integer.valueOf(amount));
		CustomerinfoDAO custdao = new CustomerinfoDAO();
		Customerinfo customerFrom = custdao.findById(customerFromId);
		Customerinfo customerTo = custdao.findById(customerToId);
		liquid.setCustomerinfoByCustomerIdFrom(customerFrom);
		liquid.setCustomerinfoByCustomerIdTo(customerTo);
		liquid.setEventDate(TimeUtils.fromIsoDate(eventDate));
		liquid.setInOrOut(0);
		liquid.setPatchNumber(patchNumber);
		ProductinfoDAO prodao = new ProductinfoDAO();
		Productinfo productinfo = prodao.findById(productId);
		liquid.setProductinfo(productinfo);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.update(liquid);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 *删除数据
	 * @throws IOException 
	 */
	public void deleteData() throws IOException{
		SalesLiquidDAO dao = new SalesLiquidDAO();
		SalesLiquid liquid = dao.findById(id);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.delete(liquid);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getPatchNumber() {
		return patchNumber;
	}

	public void setPatchNumber(String patchNumber) {
		this.patchNumber = patchNumber;
	}

	public String getCustomerFromId() {
		return customerFromId;
	}

	public void setCustomerFromId(String customerFromId) {
		this.customerFromId = customerFromId;
	}

	public String getCustomerFromName() {
		return customerFromName;
	}

	public void setCustomerFromName(String customerFromName) {
		this.customerFromName = customerFromName;
	}

	public String getCustomerToId() {
		return customerToId;
	}

	public void setCustomerToId(String customerToId) {
		this.customerToId = customerToId;
	}

	public String getCustomerToName() {
		return customerToName;
	}

	public void setCustomerToName(String customerToName) {
		this.customerToName = customerToName;
	}

	public String getAmount() {
		return amount;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}

	public String getEventDate() {
		return eventDate;
	}

	public void setEventDate(String eventDate) {
		this.eventDate = eventDate;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

}
